package org.lex.utils;

/**
 * ������ǲ��ɱ��
 * 
 * @author Daniel Liu
 */
public final class Pair<T, V> {
	private T first = null;
	private V second = null;

	public static <T, V> Pair<T, V> wrap(T first, V second) {
		return new Pair<T, V>(first, second);
	}

	public Pair() {
		this(null, null);
	}

	public Pair(T first, V second) {
		super();
		this.first = first;
		this.second = second;
	}

	public T first() {
		return first;
	}

	public V second() {
		return second;
	}

	public String toString() {
		return "[" + String.valueOf(first) + ", " + String.valueOf(second) + "]";
	}

	public Pair<T, V> clone() {
		return this;
	}

	@SuppressWarnings("unchecked")
	public boolean equals(Object o) {
		if (null == o)
			return false;
		if (o == this)
			return true;
		if (false == o instanceof Pair)
			return false;
		Pair p = (Pair) o;
		return equal(this.first, p.first) && equal(this.second, p.second);
	}

	private boolean equal(Object a, Object b) {
		if (null == a) {
			if (null == b)
				// �����е����ʣ�����null����Ϊ����ȵ���
				return true;
			return false;
		} else if (null == b) {
			return false;
		} else if (a == b)
			return true;
		else
			return a.equals(b);
	}
}
